package com.aricksm.xuan.activaty02;

import android.app.Activity;
import android.graphics.Color;
import android.media.Image;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerActivity extends Activity {

    ViewPager myViewPager;

    List<View> viewList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pager_main);

        myViewPager = (ViewPager) findViewById(R.id.my_view_pager);


        {
            View view = LayoutInflater.from(this).inflate(R.layout.item_view_pager, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.item_image_view);
            imageView.setBackgroundColor(Color.RED);
            viewList.add(view);
        }

        {
            View view = LayoutInflater.from(this).inflate(R.layout.item_view_pager, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.item_image_view);
            imageView.setBackgroundColor(Color.GREEN);
            viewList.add(view);
        }

        {
            View view = LayoutInflater.from(this).inflate(R.layout.item_view_pager, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.item_image_view);
            imageView.setBackgroundColor(Color.BLUE);
            viewList.add(view);
        }


        myViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));
            }
        });


    }


}
